const mongoose = require('mongoose')

//连接数据库
mongoose.connect('mongodb://127.0.0.1:27017/wxdb')
  .then(res => {
    console.log('数据库连接成功')
  })
  .catch(err => {
    console.log('数据库连接失败')
  })

mongoose.set('debug', true)

//1 用户信息表规则
const userInfoSchema = new mongoose.Schema({
  openid: {
    type: String,
    unique: true
  },
  nickName: String,
  avatarUrl: String,
  gender: Number,
  phone: String,
  canReceive: {
    type: Boolean,
    default: false
  },
  personInfo: {
    type: Object,
    default: null
  },
})

//创建用户信息表 model
const user = mongoose.model('UserInfo', userInfoSchema, 'userInfo')

//2 建立用户地址信息表
const addressSchema = new mongoose.Schema({
  openid: {
    type: String,
    required: true
  },
  building: String, //楼栋
  doorNum: String, //门牌号
  name: String, //收件姓名
  phone: String, //收件电话
  isDefault: { //默认地址
    type: Boolean,
    default: false
  },
  addTime: String //添加事件
})
//创建用户地址信息表model对象
const address = mongoose.model('Address', addressSchema, 'address')

//3 建立管理员信息表========================================================
const adminSchema = new mongoose.Schema({
  openid: String,
  username: String,
  password: String,
  nickName: String,
  role: String,
  time: String
})
const admin = mongoose.model('Admin', adminSchema, 'admin');
// admin.create({
//   openid: 'ocBff5Azr69zSt87UtO8nOxEaCzI',
//   username: 'admin',
//   password: '111111',
//   role: '超级管理员',
//   nickName: '張洪亮',
//   time: '2022-07-14 16:50:30'
// })

//建立订单信息表
const orderSchema = new mongoose.Schema({
  openid: String,
  startAddress: {
    type: String,
    default: ''
  },
  endAddress: {
    type: String,
    default: ''
  },
  orderType: String,
  payMoney: Number,
  publicTime: String,
  status: String,
  info: Object,
  userInfo: Object,
  acceptMan: {
    type: String,
    default: ''
  },
  acceptTime: {
    type: String,
    default: ''
  },
  confirmTime: {
    type: String,
    default: ''
  }
})
//创建地址model对象
const order = mongoose.model('Order', orderSchema, 'order')
//导出
module.exports = {
  user,
  address,
  admin,
  order
}